package com.design.oop;

import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.design.oop.bean.WheatBeerKind;
import com.design.oop.util.BookmarkPreference;
import com.design.oop.util.CommonUtil;
import com.design.oop.util.NToast;
import com.googlecode.androidannotations.annotations.EActivity;

@EActivity
public class WheatBeerDetailActivity extends Activity implements OnClickListener, OnCheckedChangeListener {
	private ImageView mEngSearchBtn;
	private ImageView mKorSearchBtn;
	private TextView mEnglishNameText;
	private TextView mKoreanNameText;
	private ImageView mImageView;
	private TextView mCountryText;
	private TextView mDegreeText;
	private TextView mRateText;
	private CheckBox importantCheck;
	private ClipboardManager cm;
	private WheatBeerKind mBeer;
	private static Toast toast;
	Set<Integer> mBookmarkSet;
	private int mDrawableId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.beer_detail);

		Bundle bundle = getIntent().getExtras();
		if (bundle == null || !bundle.containsKey("id")) {
			finish();
		}

		cm = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);

		mEngSearchBtn = (ImageView)findViewById(R.id.beer_detail_english_search_btn);
		mEngSearchBtn.setOnClickListener(this);

		mKorSearchBtn = (ImageView)findViewById(R.id.beer_detail_korean_search_btn);
		mKorSearchBtn.setOnClickListener(this);

		mEnglishNameText = (TextView)findViewById(R.id.detail_english_text);
		mEnglishNameText.setOnClickListener(this);

		mKoreanNameText = (TextView)findViewById(R.id.detail_korean_text);
		mKoreanNameText.setOnClickListener(this);

		mImageView = (ImageView)findViewById(R.id.beer_detail_image);
		mCountryText = (TextView)findViewById(R.id.beer_detail_country);
		mDegreeText = (TextView)findViewById(R.id.beer_detail_degree);
		mRateText = (TextView)findViewById(R.id.beer_detail_rate);
		importantCheck = (CheckBox)findViewById(R.id.important_memo_checkbox);
		importantCheck.setOnCheckedChangeListener(this);

		mBookmarkSet = BookmarkPreference.getBookmarkList(this);

		mDrawableId = bundle.getInt("id");
		mBeer = WheatBeerKind.getBeerByDrawable(mDrawableId);
		mEnglishNameText.setText(mBeer.getEnglishName());
		mKoreanNameText.setText(mBeer.getKoreanName());
		mImageView.setBackgroundResource(mBeer.getDrawableId());
		mCountryText.setText(mBeer.getCountry().getValue());
		mDegreeText.setText(Float.toString(mBeer.getDegree()));
		mRateText.setText(Float.toString(mBeer.getRate()));
		int index = WheatBeerKind.getIndexByDrawableId(mDrawableId);
		if (CommonUtil.isNotEmptySet(mBookmarkSet) && mBookmarkSet.contains(index)) {
			importantCheck.setChecked(true);
		} else {
			importantCheck.setChecked(false);
		}
	}

	@Override
	public void onClick(View view) {
		String url;
		Uri uri;
		Intent intent;
		if (view.getId() == R.id.beer_detail_english_search_btn) {
			url = "http://m.search.naver.com/search.naver?query=" + mBeer.getEnglishName() + "&where=m&sm=mtp_hty.idx";
			uri = Uri.parse(url);
			intent = new Intent(Intent.ACTION_VIEW, uri);
			startActivity(intent);
		} else if (view.getId() == R.id.beer_detail_korean_search_btn) {
			url = "http://m.search.naver.com/search.naver?query=" + mBeer.getKoreanName() + "&where=m&sm=mtp_hty.idx";
			uri = Uri.parse(url);
			intent = new Intent(Intent.ACTION_VIEW, uri);
			startActivity(intent);
		} else if (view.getId() == R.id.detail_english_text) {
			cm.setText(mBeer.getEnglishName());
			NToast.showToast(toast, this, makeToastMessage(mBeer.getEnglishName()), true);
		} else if (view.getId() == R.id.detail_korean_text) {
			cm.setText(mBeer.getKoreanName());
			NToast.showToast(toast, this, makeToastMessage(mBeer.getKoreanName()), true);
		}
	}

	public void onClickedCheck(View view) {
		if (importantCheck.isChecked()) {
			importantCheck.setChecked(false);
		} else {
			importantCheck.setChecked(true);
		}
	}

	private String makeToastMessage(String name) {
		return name + "\n클립보드로 복사하였습니다.";
	}

	/**
	 * @param buttonView
	 * @param isChecked
	 * @see android.widget.CompoundButton.OnCheckedChangeListener#onCheckedChanged(android.widget.CompoundButton, boolean)
	 */
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			BookmarkPreference.addBookmark(this, mDrawableId);
			mBookmarkSet = BookmarkPreference.getBookmarkList(this);
		} else {
			BookmarkPreference.removeBookmark(this, mDrawableId);
			mBookmarkSet = BookmarkPreference.getBookmarkList(this);
		}
	}
}
